<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="css/index.css">
    <style>
    </style>
</head>
<body>
<div id="app">
<!--    页头组件-->
    <el-page-header style="background-color: #0095d7;color: white;line-height: 60px" @back="goBack" content="详情页面">
    </el-page-header>
    <el-divider></el-divider>
    <el-card>
        <el-upload
                action="/upload"
                :limit="1"
                name="picFile"
                list-type="picture-card"
                :on-preview="handlePictureCardPreview"
                :on-remove="handleRemove"
                :on-success="handleSuccess"
        >
            <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="">
        </el-dialog>
    </el-card>
    <el-button @click="insert()">发布轮播图</el-button>
</div>
</body>
<!-- import Vue before Element -->
<!--引入Vue框架-->
<script src="js/vue.min.js"></script>
<!-- import JavaScript -->
<script src="js/index.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>

<script>
    let v = new Vue({
        el: '#app',
        data: function() {
            return {
                dialogImageUrl: '',
                dialogVisible: false,
                url:""
            }
        },
        methods:{
            goBack(){
                //返回上一页面
                history.back();
            },
            handleRemove(file, fileList) {
                console.log(file, fileList);
                //发请求告诉服务器删除文件夹里面的文件
                //得到要删除的文件名
                let fileName= file.response;
                console.log("文件名:"+fileName);
                axios.get("/remove?fileName="+fileName).then(function (response) {
                    console.log("服务器文件删除完成!");
                })
                v.url="";
            },
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            },
            insert(){
                if (v.url==""){
                    v.$message.error("请选择上传的图片！");
                    return;
                }
                //发出异步请求
                axios.get("/banner/insert?url="+v.url).then(function () {
                    location.href="/admin.html";
                })

            },
            handleSuccess(response,file,fileList){
                console.log("上传文件成功！");
                console.log("response="+response);
                console.log("file="+file);
                v.url=response;
            }
        }
    })
</script>
</html>